﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Sy;
using Texfina.Entity.Mg;
using RHBOQry = Texfina.BOQry.Rh;
using SYBOQry = Texfina.BOQry.Sy;
using MGBOQry = Texfina.BOQry.Mg;


namespace MT
{
    public partial class wfFichaTelaBnd : SearchPage 
   {
       public const int _GRDLIST_IDESTADO = 9;
       public const int _GRDLIST_NUPORCALERTA = 10;
    
      protected void Page_Load(object sender, EventArgs e)
      {
                       
         this.LoadInitialSettings();
         if(this.IsPostBack){
            this.LoadInPostBack();
            return;
         } 

         this.LoadData();
         SearchData();

      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label) this.Master.FindControl("lblTitle");
         lbl.Text = "Ficha Telas";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

       public override void LoadListControls()
      {
         EPlanilla objE = new EPlanilla();
         SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));
         EEmpEstable objEEmp = new EEmpEstable();
         objEEmp.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         DataTable dt = objBOEmp.GetEstablecimientos(objEEmp);
         WebHelper.LoadListControl(ddlidEstable, dt, "dsEstable", "idEstable", ConstCustomFirstListItem.ITEM_ALL);

      }

      public override void SearchData()
      {

         EPersonalBandeja objE = new EPersonalBandeja();

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         if (this.txtPersonal.Text == "")
            objE.IdPersonal = null;
         else
            objE.IdPersonal = this.txtPersonal.Text;

         if (this.txtnuDocIden.Text == "")
            objE.NuDocIden  = null;
         else
            objE.NuDocIden = this.txtnuDocIden.Text;

         if (this.txtdsPersonal.Text == "")
            objE.DsApellNom = null;
         else
            objE.DsApellNom = this.txtdsPersonal.Text;

         if (this.txtFechaDesde.Text != "")
            objE.FeFinA = Convert.ToDateTime(this.txtFechaDesde.Text);
         else
            objE.FeFinA = null;

         if (this.txtFechaDesde.Text != "")
            objE.FeFinB = Convert.ToDateTime(this.txtFechaHasta.Text);
         else
            objE.FeFinB = null;

         if (this.ddlidEstable.Text == "")
            objE.IdEstable = null;
         else
            objE.IdEstable = this.ddlidEstable.Text;

         RHBOQry.IPersonal objBO = (RHBOQry.IPersonal)WCFHelper.GetObject<RHBOQry.IPersonal>(typeof(RHBOQry.Personal));
         DataTable dt = objBO.GetByCriteriaPerContrato(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
          String strQS= "";

         switch(e.Item.Value){
            case ConstMenuToolbarValue.BUTTON_NEW:

                  DataTable dt=this.DataSource;
                  DataTable dtPorroga = dt.Clone(); 

                  foreach (GridViewRow Row in grdList.Rows)
	               {
                  
                        CheckBox chk = new CheckBox();
                        chk = (CheckBox)Row.FindControl("ChkPorroga");

                        if (chk.Checked)
                        {
                           string strfIni = Row.Cells[5].Text.Replace(ConstHTMLTagValue.GRID_CELL_EMPTY, "");
                           string strfFin = Row.Cells[6].Text.Replace(ConstHTMLTagValue.GRID_CELL_EMPTY, "");

                           if (strfIni != "" && strfFin != "")
                           {
                              string str= Row.Cells[2].Text;
                              DataRow[] dr = dt.Select("idPersonal=" + str);
                              DataRow drA = dr[0];
                              dtPorroga.ImportRow(drA);
                   
                           }
                        
                        }
	               }

                  if (dtPorroga.Rows.Count == 0)
                  {

                     string strMsg = "No existen Contratos";
                     this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfPerContratoBnd.aspx");
                     ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "msgPB", "MessageOnPostback();", true);
                     return;

                  }
  
                  Session["Porroga"] = dtPorroga;

                  strQS = "wfPorrogaBnd.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&IdPersonal=";
               break; 
         }
         Response.Redirect(strQS);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      { 
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA= FormEditAction.AddNew;

         switch(e.CommandName)
         {
             case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
             case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
             case ConstGridRowCommand.GRIDPREVIEW:
               //this.PrintReport(Convert.ToInt32(e.CommandArgument));
               return;
         }
         String strQS = "wfPerContratoMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&IdPersonal=" + dr["idPersonal"].ToString();
         Response.Redirect(strQS);
      
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              if (e.Row.Cells[_GRDLIST_IDESTADO].Text == ConstEstadoRegPersonal.PENDIENTE)
              {e.Row.ForeColor = System.Drawing.Color.Green;}

              Literal lihtml = new Literal();
              if (e.Row.Cells[_GRDLIST_NUPORCALERTA].Text != "-1")
                 lihtml.Text = "<div style='width:165px;' class='esdSpliterContent'><div class='esdSpliterValue' style=' width: " + e.Row.Cells[_GRDLIST_NUPORCALERTA].Text + "%;'></div></div>";
              else
                 lihtml.Text = "<div style='width:165px;' class='esdSpliterContentDisable'></div>";
             e.Row.Cells[8].Controls.Add(lihtml);

          }

      }

      protected void bttOk_Click(object sender, EventArgs e)
      {
          this.SearchData();
      }
   }
}